/**
 * Created by 李一方 on 2015/3/6.
 */

dojo.provide('test.PageList');
dojo.require('dijit._Widget');
dojo.require('dijit._Templated');
dojo.ready(function () {
    dojo.declare('test.PageList', [dijit._Widget, dijit._Templated], {
        templateString: ['<div class="pages">',
            '<a class="first" title="第1页" dojoAttachEvent="onclick:firstPage" dojoAttachPoint="aFirst">1..</a>',
            '<a class="prev" title="上1页" dojoAttachEvent="onclick:previousPage" dojoAttachPoint="aPre">上1页</a>',
            '<a class="prev" title="上10页" dojoAttachEvent="onclick:previousTenPage" dojoAttachPoint="aPreTen">上10页</a>',
            '<span dojoAttachPoint="pageLinkContainer"></span>',
            '<a class="next" title="下1页" dojoAttachEvent="onclick:nextPage" dojoAttachPoint="aNext">下1页</a>',
            '<a class="next" title="下10页" dojoAttachEvent="onclick:nextTenPage" dojoAttachPoint="aNextTen">下1页</a>',
            '<a class="last" title="最后1页" dojoAttachEvent="onclick:lastPage" dojoAttachPoint="aLast">..${totalPages}</a>',
            '</div>'
        ].join(''),
        totalPages: 0,
        currentPage: 1,
        onPageChange: null,
        showPages: 9,
        postCreate: function () {
            this.updateView();
        },
        updateView: function () {
            if ((this.currentPage - 1) < this.showPages / 2) {
                this.aFirst.style.display = "none";
                this.aPre.style.display = "none";
                this.aPreTen.style.display = "none";
            } else {
                this.aFirst.style.display = "inline";
                this.aPre.style.display = "inline";
                this.aPreTen.style.display = "inline";
            }
            if ((this.currentPage - this.currentPage) < this.showPages / 2) {
                this.aLast.style.display = "none";
                this.aNext.style.display = "none";
                this.aNextTen.style.display = "none";
            } else {
                this.aLast.style.display = "inline";
                this.aNext.style.display = "inline";
                this.aNextTen.style.display = "inline";
            }
            this.pageLinkContainer.innerHTML = '';
            var startPage = 1;
            if ((this.currentPage - 1) < this.showPages / 2) {
                startPage = 1;
            } else if (this.totalPages - this.currentPage < this.showPages / 2) {
                startPage = this.currentPage - this.showPages + 1;
            } else {
                startPage = this.currentPage - (this.showPages - 1) / 2
            }
            if (startPage < 1) startPage = 1;
            for (var i = startPage; i < startPage + this.showPages; i++) {
                if (i > this.totalPages) break;
                var a = dojo.create('a', {innerHTML: i}, this.pageLinkContainer, 'last');
                if (i == this.currentPage) dojo.addClass(a, 'current');
                else {
                    dojo.addClass(a, 'page');
                    this.connect(a, 'onclick', dojo.hitch(this, "gtoPage", i));
                }
            }
        },
        gotoPage: function () {
            if (pageNum < 1) this.currentPage = 1;
            else if (pageNum > this.totalPages) this.currentPage = this.totalPages;
            else this.currentPage = pageNum;
            this.updateView();
            this.onPageChange(this.currentPage);
        },
        firstPage: function () {
            this.gotoPage(1);
        },
        lastPage: function () {
            this.gotoPage(this.totalPages);
        },
        previousPage: function () {
            this.gotoPage(this.currentPage - 1);
        },
        previousTenPage: function () {
            this.gotoPage(this.currentPage - 10);
        },
        nextPage: function () {
            this.gotoPage(this.currentPage + 1);
        },
        nextTenPage: function () {
            this.gotoPage(this.currentPage + 10);
        }
    });
});
